from cls.finance.datas.tudata import TradeDatas
# from cls.finance.stock import ShareItem
from cls.orm.main import PG

class HighFall:

    def __init__(self):
        self.base = 1000
        self.startDate = None
        self.endDate = None
        self.data = TradeDatas()
        self.pg = PG()
        pass

    #  计算指数
    def calIndex(self):
        k = 0
        tDates = self.data.getTradeDate('20190101', '20190601')
        for index, row in tDates.iterrows():
            items = self.selectLastFallItems(row[0])
            nday = self.data.dTradeDays(row[0], 1)
            itemStr = "('" + "','".join(items.tolist()) + "')"
            sql = "select avg(pct_chg) from gegu.all where trade_date = '" + nday + "' and ts_code in " + itemStr
            ratio = float(self.pg.execValue(sql))
            print(len(items))
            print(nday)
            print(round(ratio,2))
            k = k + ratio

        print(k)



    #  查找出所有昨天上影线有两个点以上的个股
    def selectLastFallItems(self, date):
        # 实际上就是大涨的股票啊
        # 长上影线不一定是坏事，长下影线不一定是好事，需要看位置
        # 与大盘的相对关系来考虑
        whereRise = "(close - low) / pre_close > 0.07 and (close - open)/pre_close > 0.02 and pct_chg < 9.8 and trade_date = '" + date + "'"
        whereClause = "(high - close) / pre_close > 0.05 and  (high - open) / pre_close > 0.05 and trade_date = '" + date + "'"
        sql = 'select ts_code from gegu.all where ' + whereRise
        dates = self.pg.exec(sql)
        if len(dates) == 0:
            return []
        return dates.T.values[0]

obj = HighFall()
obj.calIndex()
